// 系统托盘
const { Tray, Menu, nativeImage, BrowserWindow } = require('electron')
const path = require('path')
let tray
let timer
let haveMessage = true
let toggle = true

// 设置顶部APP图标的操作和图标
const darkIcon = path.join(__static, 'logo.png');
const lightPath = path.join(__static, 'logo2.png');
export function setTray(win) {
    const contextMenu = Menu.buildFromTemplate([
        {
            label: '打开新窗口',
            click: () => {
                let child = new BrowserWindow({ parent: BrowserWindow.getFocusedWindow() })
                child.loadURL('https://electronjs.org');
                child.show()
            },
        },
        {type:'separator'}, // ??
        {
            label: "删除图标",
            click: () => {
                tray.destroy()
                clearInterval(timer)
            }
        }
    ])
    tray = new Tray(nativeImage.shouldUseDarkColors ? darkIcon : lightPath)
    tray.setToolTip('测试按钮')
    
    // 设置上下文
    tray.setContextMenu(contextMenu)
    if(haveMessage){
        timer = setInterval(() => {
            toggle = !toggle
            if(toggle){
                tray.setImage(nativeImage.createEmpty())
            }else{
                tray.setImage(darkIcon)
            }
        }, 600);
    }
    tray.on('double-click',()=>{
        win.isVisible()?win.hide():win.show();
    })
    // nativeImage.on('updated', () => {
    //     tray.setImage(nativeImage.shouldUseDarkColors ? darkIcon : lightIcon)
    // })
}

export function setTray2() {
    tray = new Tray(iconPath)
    tray.setToolTip('Tasky')
    tray.on('click', () => {
      if (mainWindow.isVisible()) {
        mainWindow.hide();
      } else {
        mainWindow.show();
      }
    })
    tray.on('right-click', () => {
      const menuConfig = Menu.buildFromTemplate([
        {
          label: 'Quit',
          click: () => app.quit()
        }
      ])
      tray.popUpContextMenu(menuConfig)
    })
    tray.on('drop-files', async (event, files) => {
      console.warn('files', files);
      mainWindow.webContents.send('insert-success', files);
    })
  }